home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / PIL / ArgImagePlugin.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  12.4 KB  |  387 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __version__ = '0.4'
  5. import marshal
  6. import string
  7. import Image
  8. import ImageFile
  9. import ImagePalette
  10. from PngImagePlugin import i16, i32, ChunkStream, _MODES
  11. MAGIC = '\x8aARG\r\n\x1a\n'
  12.  
  13. class ArgStream(ChunkStream):
  14.     '''Parser callbacks for ARG data'''
  15.     
  16.     def __init__(self, fp):
  17.         ChunkStream.__init__(self, fp)
  18.         self.eof = 0
  19.         self.im = None
  20.         self.palette = None
  21.         self._ArgStream__reset()
  22.  
  23.     
  24.     def __reset(self):
  25.         self.count = 0
  26.         self.id = None
  27.         self.action = ('NONE',)
  28.         self.images = { }
  29.         self.names = { }
  30.  
  31.     
  32.     def chunk_AHDR(self, offset, bytes):
  33.         '''AHDR -- animation header'''
  34.         if self.count != 0:
  35.             raise SyntaxError, 'misplaced AHDR chunk'
  36.         self.count != 0
  37.         s = self.fp.read(bytes)
  38.         self.size = (i32(s), i32(s[4:]))
  39.         
  40.         try:
  41.             (self.mode, self.rawmode) = _MODES[(ord(s[8]), ord(s[9]))]
  42.         except:
  43.             raise SyntaxError, 'unknown ARG mode'
  44.  
  45.         if Image.DEBUG:
  46.             print 'AHDR size', self.size
  47.             print 'AHDR mode', self.mode, self.rawmode
  48.         
  49.         return s
  50.  
  51.     
  52.     def chunk_AFRM(self, offset, bytes):
  53.         '''AFRM -- next frame follows'''
  54.         if self.count != 0:
  55.             raise SyntaxError, 'misplaced AFRM chunk'
  56.         self.count != 0
  57.         self.show = 1
  58.         self.id = 0
  59.         self.count = 1
  60.         self.repair = None
  61.         s = self.fp.read(bytes)
  62.         if len(s) >= 2:
  63.             self.id = i16(s)
  64.             if len(s) >= 4:
  65.                 self.count = i16(s[2:4])
  66.                 if len(s) >= 6:
  67.                     self.repair = i16(s[4:6])
  68.                 else:
  69.                     self.repair = None
  70.             
  71.         
  72.         if Image.DEBUG:
  73.             print 'AFRM', self.id, self.count
  74.         
  75.         return s
  76.  
  77.     
  78.     def chunk_ADEF(self, offset, bytes):
  79.         '''ADEF -- store image'''
  80.         if self.count != 0:
  81.             raise SyntaxError, 'misplaced ADEF chunk'
  82.         self.count != 0
  83.         self.show = 0
  84.         self.id = 0
  85.         self.count = 1
  86.         self.repair = None
  87.         s = self.fp.read(bytes)
  88.         if len(s) >= 2:
  89.             self.id = i16(s)
  90.             if len(s) >= 4:
  91.                 self.count = i16(s[2:4])
  92.             
  93.         
  94.         if Image.DEBUG:
  95.             print 'ADEF', self.id, self.count
  96.         
  97.         return s
  98.  
  99.     
  100.     def chunk_NAME(self, offset, bytes):
  101.         '''NAME -- name the current image'''
  102.         if self.count == 0:
  103.             raise SyntaxError, 'misplaced NAME chunk'
  104.         self.count == 0
  105.         name = self.fp.read(bytes)
  106.         self.names[self.id] = name
  107.         return name
  108.  
  109.     
  110.     def chunk_AEND(self, offset, bytes):
  111.         '''AEND -- end of animation'''
  112.         if Image.DEBUG:
  113.             print 'AEND'
  114.         
  115.         self.eof = 1
  116.         raise EOFError, 'end of ARG file'
  117.  
  118.     
  119.     def __getmodesize(self, s, full = 1):
  120.         size = (i32(s), i32(s[4:]))
  121.         
  122.         try:
  123.             (mode, rawmode) = _MODES[(ord(s[8]), ord(s[9]))]
  124.         except:
  125.             raise SyntaxError, 'unknown image mode'
  126.  
  127.         if full:
  128.             if ord(s[12]):
  129.                 pass
  130.             
  131.             if ord(s[11]):
  132.                 raise SyntaxError, 'unknown filter category'
  133.             ord(s[11])
  134.         
  135.         return (size, mode, rawmode)
  136.  
  137.     
  138.     def chunk_PAST(self, offset, bytes):
  139.         '''PAST -- paste one image into another'''
  140.         if self.count == 0:
  141.             raise SyntaxError, 'misplaced PAST chunk'
  142.         self.count == 0
  143.         if self.repair is not None:
  144.             self.images[self.id] = self.images[self.repair].copy()
  145.             self.repair = None
  146.         
  147.         s = self.fp.read(bytes)
  148.         im = self.images[i16(s)]
  149.         x = i32(s[2:6])
  150.         y = i32(s[6:10])
  151.         bbox = (x, y, im.size[0] + x, im.size[1] + y)
  152.         if im.mode in ('RGBA',):
  153.             self.images[self.id].paste(im, bbox, im)
  154.         else:
  155.             self.images[self.id].paste(im, bbox)
  156.         self.action = ('PAST',)
  157.         self._ArgStream__store()
  158.         return s
  159.  
  160.     
  161.     def chunk_BLNK(self, offset, bytes):
  162.         '''BLNK -- create blank image'''
  163.         if self.count == 0:
  164.             raise SyntaxError, 'misplaced BLNK chunk'
  165.         self.count == 0
  166.         s = self.fp.read(bytes)
  167.         (size, mode, rawmode) = self._ArgStream__getmodesize(s, 0)
  168.         self.action = ('BLNK',)
  169.         self.im = Image.core.fill(mode, size, 0)
  170.         self._ArgStream__store()
  171.         return s
  172.  
  173.     
  174.     def chunk_IHDR(self, offset, bytes):
  175.         '''IHDR -- full image follows'''
  176.         if self.count == 0:
  177.             raise SyntaxError, 'misplaced IHDR chunk'
  178.         self.count == 0
  179.         s = self.fp.read(bytes)
  180.         (size, mode, rawmode) = self._ArgStream__getmodesize(s)
  181.         self.action = ('IHDR',)
  182.         self.im = Image.core.new(mode, size)
  183.         self.decoder = Image.core.zip_decoder(rawmode)
  184.         self.decoder.setimage(self.im, (0, 0) + size)
  185.         self.data = ''
  186.         return s
  187.  
  188.     
  189.     def chunk_DHDR(self, offset, bytes):
  190.         '''DHDR -- delta image follows'''
  191.         if self.count == 0:
  192.             raise SyntaxError, 'misplaced DHDR chunk'
  193.         self.count == 0
  194.         s = self.fp.read(bytes)
  195.         (size, mode, rawmode) = self._ArgStream__getmodesize(s)
  196.         diff = ord(s[13])
  197.         offs = (i32(s[14:18]), i32(s[18:22]))
  198.         bbox = offs + (offs[0] + size[0], offs[1] + size[1])
  199.         if Image.DEBUG:
  200.             print 'DHDR', diff, bbox
  201.         
  202.         self.action = ('DHDR', diff, bbox)
  203.         self.im = Image.core.new(mode, size)
  204.         self.decoder = Image.core.zip_decoder(rawmode)
  205.         self.decoder.setimage(self.im, (0, 0) + size)
  206.         self.data = ''
  207.         return s
  208.  
  209.     
  210.     def chunk_JHDR(self, offset, bytes):
  211.         '''JHDR -- JPEG image follows'''
  212.         if self.count == 0:
  213.             raise SyntaxError, 'misplaced JHDR chunk'
  214.         self.count == 0
  215.         s = self.fp.read(bytes)
  216.         (size, mode, rawmode) = self._ArgStream__getmodesize(s, 0)
  217.         self.action = ('JHDR',)
  218.         self.im = Image.core.new(mode, size)
  219.         self.decoder = Image.core.jpeg_decoder(rawmode)
  220.         self.decoder.setimage(self.im, (0, 0) + size)
  221.         self.data = ''
  222.         return s
  223.  
  224.     
  225.     def chunk_UHDR(self, offset, bytes):
  226.         '''UHDR -- uncompressed image data follows (EXPERIMENTAL)'''
  227.         if self.count == 0:
  228.             raise SyntaxError, 'misplaced UHDR chunk'
  229.         self.count == 0
  230.         s = self.fp.read(bytes)
  231.         (size, mode, rawmode) = self._ArgStream__getmodesize(s, 0)
  232.         self.action = ('UHDR',)
  233.         self.im = Image.core.new(mode, size)
  234.         self.decoder = Image.core.raw_decoder(rawmode)
  235.         self.decoder.setimage(self.im, (0, 0) + size)
  236.         self.data = ''
  237.         return s
  238.  
  239.     
  240.     def chunk_IDAT(self, offset, bytes):
  241.         '''IDAT -- image data block'''
  242.         s = self.fp.read(bytes)
  243.         self.data = self.data + s
  244.         (n, e) = self.decoder.decode(self.data)
  245.         if n < 0:
  246.             if e < 0:
  247.                 raise IOError, 'decoder error %d' % e
  248.             e < 0
  249.         else:
  250.             self.data = self.data[n:]
  251.         return s
  252.  
  253.     
  254.     def chunk_DEND(self, offset, bytes):
  255.         return self.chunk_IEND(offset, bytes)
  256.  
  257.     
  258.     def chunk_JEND(self, offset, bytes):
  259.         return self.chunk_IEND(offset, bytes)
  260.  
  261.     
  262.     def chunk_UEND(self, offset, bytes):
  263.         return self.chunk_IEND(offset, bytes)
  264.  
  265.     
  266.     def chunk_IEND(self, offset, bytes):
  267.         '''IEND -- end of image'''
  268.         del self.decoder
  269.         del self.data
  270.         self._ArgStream__store()
  271.         return self.fp.read(bytes)
  272.  
  273.     
  274.     def __store(self):
  275.         cid = self.action[0]
  276.         if cid in ('BLNK', 'IHDR', 'JHDR', 'UHDR'):
  277.             self.images[self.id] = self.im
  278.         elif cid == 'DHDR':
  279.             (cid, mode, bbox) = self.action
  280.             im0 = self.images[self.id]
  281.             im1 = self.im
  282.             if mode == 0:
  283.                 im1 = im1.chop_add_modulo(im0.crop(bbox))
  284.             
  285.             im0.paste(im1, bbox)
  286.         
  287.         self.count = self.count - 1
  288.         if self.count == 0 and self.show:
  289.             self.im = self.images[self.id]
  290.             raise EOFError
  291.         self.show
  292.  
  293.     
  294.     def chunk_PLTE(self, offset, bytes):
  295.         '''PLTE -- palette data'''
  296.         s = self.fp.read(bytes)
  297.         if self.mode == 'P':
  298.             self.palette = ImagePalette.raw('RGB', s)
  299.         
  300.         return s
  301.  
  302.     
  303.     def chunk_sYNC(self, offset, bytes):
  304.         '''SYNC -- reset decoder'''
  305.         if self.count != 0:
  306.             raise SyntaxError, 'misplaced sYNC chunk'
  307.         self.count != 0
  308.         s = self.fp.read(bytes)
  309.         self._ArgStream__reset()
  310.         return s
  311.  
  312.  
  313.  
  314. def _accept(prefix):
  315.     return prefix[:8] == MAGIC
  316.  
  317.  
  318. class ArgImageFile(ImageFile.ImageFile):
  319.     format = 'ARG'
  320.     format_description = 'Animated raster graphics'
  321.     
  322.     def _open(self):
  323.         if self.fp.read(8) != MAGIC:
  324.             raise SyntaxError, 'not an ARG file'
  325.         self.fp.read(8) != MAGIC
  326.         self.arg = ArgStream(self.fp)
  327.         (cid, offset, bytes) = self.arg.read()
  328.         if cid != 'AHDR':
  329.             raise SyntaxError, 'expected an AHDR chunk'
  330.         cid != 'AHDR'
  331.         s = self.arg.call(cid, offset, bytes)
  332.         self.arg.crc(cid, s)
  333.         self.mode = self.arg.mode
  334.         self.size = self.arg.size
  335.  
  336.     
  337.     def load(self):
  338.         if self.arg.im is None:
  339.             self.seek(0)
  340.         
  341.         self.im = self.arg.im
  342.         self.palette = self.arg.palette
  343.         Image.Image.load(self)
  344.  
  345.     
  346.     def seek(self, frame):
  347.         if self.arg.eof:
  348.             raise EOFError, 'end of animation'
  349.         self.arg.eof
  350.         self.fp = self.arg.fp
  351.         while None:
  352.             (cid, offset, bytes) = self.arg.read()
  353.             if self.arg.eof:
  354.                 raise EOFError, 'end of animation'
  355.             
  356.             try:
  357.                 s = self.arg.call(cid, offset, bytes)
  358.             except EOFError:
  359.                 break
  360.             except 'glurk':
  361.                 if Image.DEBUG:
  362.                     print cid, bytes, '(unknown)'
  363.                 
  364.                 s = self.fp.read(bytes)
  365.  
  366.             self.arg.crc(cid, s)
  367.             continue
  368.             self.fp.read(4)
  369.             return None
  370.  
  371.     
  372.     def tell(self):
  373.         return 0
  374.  
  375.     
  376.     def verify(self):
  377.         '''Verify ARG file'''
  378.         self.fp.seek(8)
  379.         self.arg.verify(self)
  380.         self.arg.close()
  381.         self.fp = None
  382.  
  383.  
  384. Image.register_open('ARG', ArgImageFile, _accept)
  385. Image.register_extension('ARG', '.arg')
  386. Image.register_mime('ARG', 'video/x-arg')
  387.